<template>
  <div>
    <modal
      title="删除文章"
      :visible="isModal"
      @modal-on-close="isModal = false"
      @modal-on-confirm="hideAndDelete"
    >
      <p>确定要删除吗？</p>
    </modal>
    <article class="mx-auto mb-5 pb-3" style="width: 700px">
      <breadcrumb :title="artical.title" :columnId="artical.column" />
      <h3 class="mb-4">{{ artical.title }}</h3>
      <hr />
      <div class="d-flex align-items-center mb-4">
        <user-profile
          :user="artical.author"
          v-if="typeof artical.author === 'object'"
        />
        <!-- v-if="typeof artical.author === 'object'" 为什么要加这个判断呢？ -->
        <span class="text-muted col" style="text-align: right"
          >创建时间：{{ artical.createdAt }}</span
        >
      </div>
      <hr />
      <img :src="imageUrl" alt="" class="rounded m-auto center-block mb-4" />
      <div v-html="currentHTML"></div>
      <template v-if="isEditable">
        <router-link
          :to="{ name: 'create', query: { id: artical._id } }"
          type="button"
          class="btn btn-outline-primary"
          style="margin-right: 10px"
          >编辑</router-link
        >
        <button
          type="button"
          class="btn btn-outline-danger"
          @click.prevent="isModal = true"
        >
          删除
        </button>
      </template>
    </article>
  </div>
</template>
<script lang='ts'>
import { computed, defineComponent, reactive, ref, onMounted } from "vue";
import { useRoute, useRouter } from "vue-router";
import { useStore } from "vuex";
import MarkdownIt from "markdown-it";
import { ArticalProps, UserProps } from "@/utils/interface";
import UserProfile from "@/components/UserProfile.vue";
import Breadcrumb from "@/components/Breadcrumb.vue";
import Modal from "@/components/Modal.vue";
import createMessage from "@/utils/createMessage";
export default defineComponent({
  name: "ArticalDetail",
  components: { UserProfile, Breadcrumb, Modal },
  setup() {
    const route = useRoute();
    const router = useRouter();
    const store = useStore();
    const md = new MarkdownIt();
    const currentId = route.params.id;
    const isModal = ref(false);
    store.dispatch("fetchArtical", currentId);
    const artical = computed<ArticalProps>(() => {
      return store.getters.getArtical;
    });
    const imageUrl = computed(() => {
      if (artical.value.image) {
        return artical.value.image.url + "?x-oss-process=image/resize,w_700";
      } else {
        return null;
      }
    });
    const currentHTML = computed(() => {
      const { content, isHTML } = artical.value;
      if (artical.value && content) {
        return isHTML ? content : md.render(content);
      } else {
        return "";
      }
    });

    const isEditable = computed(() => {
      const { isLogin, _id } = store.state.user;
      if (artical.value && artical.value.author && isLogin) {
        // 拿到当前文章的作者信息
        const articalAuthor = artical.value.author as UserProps;
        // 比较当前文章作者id与登录账号的作者id，是，则可以进行编辑和删除操作
        return articalAuthor._id === _id;
      } else {
        return false;
      }
    });

    const hideAndDelete = () => {
      isModal.value = false;
      store.dispatch("deletePost", currentId).then((rawData) => {
        createMessage("删除成功，2秒后跳转到专栏首页", "success", 2000);
        setTimeout(() => {
          router.push({
            name: "columndetail",
            params: { id: rawData.data.column },
          });
        }, 2000);
      });
    };
    return {
      artical,
      imageUrl,
      currentHTML,
      isEditable,
      isModal,
      hideAndDelete,
    };
  },
});
</script>
<style scoped>
</style>